package com.jason.item.model;

import com.jason.item.dao.entity.Item;
import com.jason.item.dao.entity.ItemStock;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.*;
import java.math.BigDecimal;

/**
 * 商品 模型
 *
 */
@Data
public class ItemModel {

    private Integer id;


    @NotBlank(message = "商品标题不能为空")
    private String title;

    @NotBlank(message = "商品描述不能为空")
    private String description;
    @NotNull(message = "商品价格不能为空")
    @Min(value = 0, message = "商品价格必须大于0")
    private BigDecimal price;

    //库存
    @NotNull(message = "商品库存不能为空")
    @Min(value = 0, message = "商品库存必须大于0")
    private Integer stock;

    //销量
    private Integer sales = 0;
    // 图片
    @NotBlank(message = "商品图片不能为空")
    private String imgUrl;

    public static ItemModel convertByItem(Item item) {
        ItemModel itemModel = new ItemModel();
        BeanUtils.copyProperties(item, itemModel);
        return itemModel;
    }

    public Item getItem() {
        Item item = new Item();
        BeanUtils.copyProperties(this, item);
        return item;
    }

    public ItemStock getItemStock() {
        ItemStock itemStock = new ItemStock();
        itemStock.setStock(this.stock);
        itemStock.setItemId(this.id);
        return itemStock;
    }
}
